﻿using System;
using Kiiro.Logging;
using Microsoft.SharePoint;

namespace Kiiro.ExceptionManagement
{
    public class ExceptionManager
    {
        /// <summary>
        /// Log an exception to <c>logfile</c>
        /// </summary>
        /// <param name="ex">The exception to handle.</param>
        /// <param name="rethrow">Rethrow the exception.</param>
        public static void HandleException ( Exception ex, bool rethrow )
        {
            string line = string.Format ( "\r\n{0}\r\n{1}\r\n", ex.Message, ex.StackTrace );
            LoggingManager.LogMessage ( ex.GetType ( ).ToString ( ), line );
            if ( rethrow )
            {
                throw ex;
            }
        }

        /// <summary>
        /// Log a SharePoint exception to <c>logofile</c>
        /// </summary>
        /// <param name="ex">A SharePoint exception.</param>
        /// <param name="rethrow">Rethrow the exception.</param>
        public static void HandleException ( SPException ex, bool rethrow )
        {
            string line = string.Format ( "\r\n{0}\r\n{1}\r\n", ex.Message, ex.StackTrace );
            LoggingManager.LogMessage ( ex.GetType ( ).ToString ( ), line );
            if ( rethrow )
            {
                throw ex;
            }
        }

        /// <summary>
        /// Log an Unauthorized Access exception to <c>logofile</c>
        /// </summary>
        /// <param name="ex">An Unauthorized Access exception.</param>
        /// <param name="rethrow">Rethrow the exception.</param>
        public static void HandleException ( UnauthorizedAccessException ex, bool rethrow )
        {
            string line = string.Format ( "\r\n{0}\r\n{1}\r\n", ex.Message, ex.StackTrace );
            LoggingManager.LogMessage ( ex.GetType ( ).ToString ( ), line );
            if ( rethrow )
            {
                throw ex;
            }
        }

        /// <summary>
        /// Log a Kiiro Provisioner exception to <c>logofile</c>
        /// </summary>
        /// <param name="ex">A Kiiro Provisioner exception.</param>
        /// <param name="rethrow">Rethrow the exception.</param>
        public static void HandleException ( KiiroProvisionerException ex, bool rethrow )
        {
            string line = string.Format ( "\r\n{0}\r\n{1}\r\n", ex.Message, ex.StackTrace );
            LoggingManager.LogMessage ( ex.GetType ( ).ToString ( ), line );
            if ( rethrow )
            {
                throw ex;
            }
        }

        /// <summary>
        /// Log a Kiiro Unauthorized Access exception to <c>logofile</c>
        /// </summary>
        /// <param name="ex">A Kiiro Unauthorized Access exception.</param>
        /// <param name="rethrow">Rethrow the exception.</param>
        public static void HandleException(KiiroUnauthorizedAccessException ex, bool rethrow)
        {
            string line = string.Format ( "\r\n{0}\r\n{1}\r\n", ex.Message, ex.StackTrace );
            LoggingManager.LogMessage ( ex.GetType ( ).ToString ( ), line );
            if ( rethrow )
            {
                throw ex;
            }
        }
        
        /// <summary>
        /// Log an exception to <c>logfile</c>
        /// </summary>
        /// <param name="ex">The exception to handle.</param>
        /// <param name="rethrow">Rethrow the exception.</param>
        public static void HandleException ( Exception ex, bool rethrow, string notes )
        {
            string line = string.Format ( "\r\n{0}\r\n{1}\r\n{2}", notes, ex.Message, ex.StackTrace );
            LoggingManager.LogMessage ( ex.GetType ( ).ToString ( ), line );
            if ( rethrow )
            {
                throw ex;
            }
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.ExceptionManagement/ExceptionManager.cs $
 * 
 * 4     19/11/09 5:01p Camerons
 * 
 * 3     17/08/09 10:45a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/